if (typeof SHARETHIS !='undefined') {
  var lsShare = SHARETHIS.addEntry(
  		{title:'SHARE'},
  		{button:false}
  	);
}
jQuery(document).ready(function(){
	if (jQuery('#ls-share').length>=1) {
  	var shareBtn = jQuery('#ls-share');
  	lsShare.attachButton(shareBtn[0]);
	}
	jQuery('a#start-discussion-link').click(function(){
		jQuery.scrollTo( '#start-discussion', 800 );
		return false;
	});

	//reply comments button
	if (jQuery('#comments-reply-box').length>=1) {
  	comment_form = '<div class="comment-reply-form-box">'+jQuery('#comments-reply-box').html()+'</div>';
  	jQuery('#comments-reply-box').remove();
  	jQuery('.comments-left .discussions .buttons .comment-reply').click(function(e){
  		showReplyForm(this);
  		return false;
  	});
	}
	//slider
	if (jQuery('.photogallery .gal-img').length>=1) {
  	var mainSlider = jQuery('.photogallery .gal-img').slider({
  	  callback : mainSlider_cb,
  	  scroll: 1, 
  	  visible:1,
  	  li:605
  	});
	}
	
	if (jQuery('#featured-img li').length>=1) {
  	var rrSlider = jQuery('#featured-img').slider({
  	  callback : rrSlider_cb,
  	  scroll: 1, 
  	  visible:1,
  	  li:336
  	});
	}
	
	jQuery('.go-to-comments').click(function(){
		if (jQuery('#late-new').length<=0) {
		  commentsDiv = '#start-discussion';
		}else {
		  commentsDiv = '#late-new';
		}
		jQuery.scrollTo( commentsDiv, 800 );
	});
	
	var action = jQuery.query.get('action');
	if (action!='') {
		if (jQuery('#'+action).length <=0) {
		  scrollDiv = '#start-discussion';
		}else {
			scrollDiv = '#'+action;
		}
		jQuery.scrollTo(scrollDiv, 800 );
	}
	
	jQuery('.goTo-single').click(function(){
		goTourl = jQuery(this).attr('single_href');
		window.top.location=goTourl;
		return false;
	});
	jQuery('#browse-posts .browse-nav li a').click(function(){
		contentLi = jQuery(this).parent('li');
		contentID = jQuery(contentLi).attr('id').replace('-tab','');
		jQuery('#browse-posts .browse-nav li a').removeClass('tab-active');
		updateContent('category-content',contentID);
		return false;
	});
	jQuery('#pop-posts .browse-nav li a').click(function(){
		contentLi = jQuery(this).parent('li');
		contentID = jQuery(contentLi).attr('id').replace('-tab','');
		jQuery('#pop-posts .browse-nav li a').removeClass('tab-active');
		updateContent('most-x',contentID);
		return false;
	});
	
	jQuery('#ls-content #contributors li a').hover(
		//onMouseover:
		function(e){
			var nameText = jQuery(this).html();
			var imageLink = jQuery(this).attr("alt");
			jQuery('#contributor-popup > p.name').html('<a href="'+this.href+'">'+nameText+'</a>');
			jQuery('#contributor-popup > a').attr('href',this.href);
			jQuery('#contributor-popup .viewposts a').attr('href',this.href);
			//forces null source when alt attribute
			if(imageLink != ""){
				jQuery('#popup-image').attr("src",jQuery(this).attr("alt"));
			}
			else{
					jQuery('#popup-image').attr("src","");
			}
			// image not shown  when no source.
			if(jQuery('#popup-image').attr("src") == ""){
			 	jQuery('#popup-image').css("display","none");
			}
			else{
				jQuery('#popup-image').css("display","block");
			}
			jQuery("#contributor-popup").appendTo(jQuery(this).parent());
			jQuery('#contributor-popup').css({
			  'display' : 'block', 
			  'position' : 'absolute',
			  'left' : '-156px',
			  'top' : '-106px'
				
			});
			nameText = jQuery(this).html().toString();
		},
		//onMouseOut:
		function() {
			//slide = jQuery(this).attr('slideId');
			
		}
	);
	jQuery('#contributor-popup , .contributors-1 , .contributors-2').bind('mouseleave', function(){ 
		jQuery('#contributor-popup').css('display','none');
		jQuery('#popup-image').attr("src",jQuery(this).attr("src",""));
	});    
	
});

function showReplyForm(obj) {
	var buttonDiv = jQuery(obj).parent('div.buttons');
	var nameDiv = jQuery(buttonDiv).parent('.date-buttons').prev().prev('.name');
	var arrowDiv = jQuery(nameDiv).prev('div.arrow');
	var targetSpace = nameDiv.parent().next('br.clear');
	var linkHash = obj.hash.replace('#','');
	var values = linkHash.split('_');

	jQuery(buttonDiv).hide();
	//jQuery(nameDiv).addClass('replying');
	//jQuery(arrowDiv).addClass('arrow-replying');
	jQuery(targetSpace).after(comment_form);

	var formBox = jQuery(targetSpace).next('.comment-reply-form-box').children('.replyformTop');
	jQuery(formBox).attr('class', 'replyformTop '+values[1]+'-reply');
	var comment_parent = jQuery(targetSpace).next('.comment-reply-form-box').children('.replyformTop').children('.leave-comments-form').children('input[name="comment_parent"]');
	jQuery(comment_parent).val(values[0]);
	jQuery('.comment-reply-form-box').show();
	//limitChars('textarea.leave-com-mid');
}

function hideReplyForm(t) {
	var replyDiv = jQuery(t).parent('div.submit-cancel').parent('.leave-comments-form').parent('.replyformTop').parent('.comment-reply-form-box');
	var commentReply = jQuery(replyDiv).prev('br.clear').prev('.comment-box').children('.name');
	
	var arrowReply = jQuery(commentReply).prev('.arrow');
    jQuery('.buttons').show();
	//commentReply.removeClass('replying');
	//arrowReply.removeClass('arrow-replying');
	
	replyDiv.next('br.clear').remove();
	replyDiv.remove();
}

function submitComment(obj,divID) {
	var targetForm = jQuery(obj).parent().parent();
	if (divID =='#start-discussion') {
		var replyDiv = jQuery(targetForm).parent('#start-discussion');
		var thankyouDiv = '#start-discussion';
	}else {
		var replyDiv = jQuery(targetForm).parent('.replyformTop');
		var thankyouDiv = '.comment-reply-form-box';
	}
	
	var parentID = jQuery(targetForm).children('#comment_parent').val();
	var replyID = 'currentReply-'+parentID;
		jQuery(targetForm).addClass(replyID);
	var errorDiv = jQuery('form.'+replyID).prev().prev('div.messages');
	var options = {
		dataType: 'json',
		method: 'POST',
		success: function(data) {
			if (data.success==false) {
				//reset the colors first
				jQuery('form.'+replyID+' input, form.'+replyID+' textarea').removeClass('error');
				var errorMsg ='';
				jQuery.each(data.errors, function(i,val) {
					jQuery('form.'+replyID+' input[name="'+i+'"], form.'+replyID+' textarea[name="'+i+'"]').addClass('error');
					errorMsg += '<p>'+val+'</p>';
				});
				jQuery(errorDiv).html(errorMsg);
				jQuery(errorDiv).show();
			}else if (data.success ==true) {
				jQuery(replyDiv).removeClass('replyformTop');
				var replyClass = jQuery(replyDiv).attr('class') ? jQuery(replyDiv).attr('class') : 'first-level-reply' ;
				
				var successMsg = '';
					successMsg +='<div class="'+replyClass+'">';
						successMsg +='<div class="arrow"></div>	';
				 			successMsg +='<h3>Thank You!</h3>';
								successMsg +='<p>Your comment has been submitted and should appear shortly.</p>';
					successMsg +='</div>';
				
				jQuery(replyDiv).slideUp(800,function(){ 
					jQuery(thankyouDiv).html(successMsg);
					if (divID=='#start-discussion') {
						jQuery(thankyouDiv).addClass('comments-left');
						jQuery(thankyouDiv).css('border','none');
					}
					jQuery(thankyouDiv).show();
				});
			}
		}
	};
	jQuery('.'+replyID).ajaxSubmit(options);
}
function mainSlider_cb(obj) {
	jQuery('.photogallery .gal-img li').css('display','block');
    //get items height and set all of the items to have same height
    //to avoid extra white spaces
    var itemsHeight = jQuery('.gal-img li:eq(0) img').height();
    jQuery('.gal-img li').height( (itemsHeight+6));
	jQuery('.gal-img-controls #prev a').click(function() {
    obj.slidePrev();
    if (obj.opts.slideLeft) {
    	//make next active
      if (obj.opts.index < (obj.totalPage)-1) {
      	 if (jQuery('.gal-img-controls #next a.disabled-btn').length >=1) {
      		 jQuery('.gal-img-controls #next a.disabled-btn').removeClass('disabled-btn');
      	 }
      }
      //make prev inactive
      if (obj.opts.index <=0) {
      	jQuery('.gal-img-controls #prev a').addClass('disabled-btn');
      }
      //update description and image index
      var imgDescr = jQuery('.gal-img li:eq('+obj.opts.index+') img').attr('alt');
      jQuery('.gal-img-controls .caption').html(imgDescr);
      jQuery('.gal-img-controls .slide-number').html('Image '+(obj.opts.index+1)+' of '+obj.totalPage);
      
      //get items height and set all of the items to have same height
      //to avoid extra white spaces
      var itemsHeight = jQuery('.gal-img li:eq('+obj.opts.index+') img').height();
      jQuery('.gal-img li').height( (itemsHeight+6)); 
    }
    
    return false;
  });
  jQuery('.gal-img-controls #next a').click(function() {
    obj.slideNext();
    if (obj.opts.slideRight) {
    	 //make prev active
      if (obj.opts.index >0) {
      	if (jQuery('.gal-img-controls #prev a.disabled-btn').length >=1) {
      		 jQuery('.gal-img-controls #prev a.disabled-btn').removeClass('disabled-btn');
      	 }
      }
      //make next inactive
      if (obj.opts.index >= (obj.totalPage)-1) {
      	jQuery('.gal-img-controls #next a').addClass('disabled-btn');
      }
      //update description and image index
      var imgDescr = jQuery('.gal-img li:eq('+obj.opts.index+') img').attr('alt');
      jQuery('.gal-img-controls .caption').html(imgDescr);
      jQuery('.gal-img-controls .slide-number').html('Image '+(obj.opts.index+1)+' of '+obj.totalPage);
      
      //get items height and set all of the items to have same height
      //to avoid extra white spaces
      var itemsHeight = jQuery('.gal-img li:eq('+obj.opts.index+') img').height();
      jQuery('.gal-img li').height( (itemsHeight+6)); 
    }
    return false;
  });
}

function rrSlider_cb(obj) {
	jQuery('#featured-img li').css('display','block');
	//update first rr descr
	var firstDescr = jQuery('#featured-img li:eq(0) img').attr('alt');
	var firstLink = jQuery('#featured-img li:eq('+obj.opts.index+') img').attr('link');
  jQuery('#featured-img-controls .caption').html('<a href="'+firstLink+'">'+firstDescr+'</a>');
  jQuery('#featured-img-controls .slide-number').html('Slideshow 1 of '+obj.totalPage);
  
  //get items height and set all of the items to have same height
  //to avoid extra white spaces
  var itemsHeight = jQuery('#featured-img li:eq(0) img').height();
  jQuery('#featured-img li').height( (itemsHeight+6));
  
	jQuery('#featured-img-controls #prev a').click(function() {
    obj.slidePrev();
    if (obj.opts.slideLeft) {
    	//make next active
      if (obj.opts.index < (obj.totalPage)-1) {
      	 if (jQuery('#featured-img-controls #next a.disabled-btn').length >=1) {
      		 jQuery('#featured-img-controls #next a.disabled-btn').removeClass('disabled-btn');
      	 }
      }
      //make prev inactive
      if (obj.opts.index <=0) {
      	jQuery('#featured-img-controls #prev a').addClass('disabled-btn');
      }
      //update description and image index
      var imgDescr = jQuery('#featured-img li:eq('+obj.opts.index+') img').attr('alt');
      var imgLink = jQuery('#featured-img li:eq('+obj.opts.index+') img').attr('link');
      jQuery('#featured-img-controls .caption').html('<a href="'+imgLink+'">'+imgDescr+'</a>');
      jQuery('#featured-img-controls .slide-number').html('Slideshow '+(obj.opts.index+1)+' of '+obj.totalPage);
      
      //get items height and set all of the items to have same height
      //to avoid extra white spaces
      var itemsHeight = jQuery('#featured-img li:eq('+obj.opts.index+') img').height();
      jQuery('#featured-img li').height( (itemsHeight+6));
    }
    
    return false;
  });
  jQuery('#featured-img-controls #next a').click(function() {
    obj.slideNext();
    if (obj.opts.slideRight) {
    	 //make prev active
      if (obj.opts.index >0) {
      	if (jQuery('#featured-img-controls #prev a.disabled-btn').length >=1) {
      		 jQuery('#featured-img-controls #prev a.disabled-btn').removeClass('disabled-btn');
      	 }
      }
      //make next inactive
      if (obj.opts.index >= (obj.totalPage)-1) {
      	jQuery('#featured-img-controls #next a').addClass('disabled-btn');
      }
      //update description and image index
      var imgDescr = jQuery('#featured-img li:eq('+obj.opts.index+') img').attr('alt');
      var imgLink = jQuery('#featured-img li:eq('+obj.opts.index+') img').attr('link');
      jQuery('#featured-img-controls .caption').html('<a href="'+imgLink+'">'+imgDescr+'</a>');
      jQuery('#featured-img-controls .slide-number').html('Slideshow '+(obj.opts.index+1)+' of '+obj.totalPage);
      
      //get items height and set all of the items to have same height
      //to avoid extra white spaces
      var itemsHeight = jQuery('#featured-img li:eq('+obj.opts.index+') img').height();
      jQuery('#featured-img li').height( (itemsHeight+6)); 
    }
    return false;
  });
}       

/* for sidebar info swapping */
function updateContent(id,name){
	jQuery("#" + name +"-tab a").addClass('tab-active');
	switch(name){
		case "categories":
			htmlContent = jQuery('#category-content-info').html();
			jQuery('#browse-content-list').html(htmlContent);	
			//jQuery("#categories-tab a").css("padding","14px 17px 20px; !important");
			break;
		case "archives":
			htmlContent = jQuery('#category-archive').html();
			jQuery('#browse-content-list').html(htmlContent);	
			break;
		case "commented":
			htmlContent = jQuery('#most-commented-list').html();
			jQuery('#'+id).html(htmlContent);
			break;
		case "viewed":
			htmlContent = jQuery('#most-views').html();
			jQuery('#'+id).html(htmlContent);
			break;
		case "tags":
			htmlContent = jQuery('#category-popular-tags').html();
			jQuery('#browse-content-list').html(htmlContent);
		break;
	}
}
	
function expandReplies(obj,cid,cssClass,comment_status) {
	var values = {
			action: 'expand-comments',
			cid: cid,
			level:cssClass,
			comment_status: comment_status
	}
	postUrl = 'http://'+window.location.host+'/wp-content/plugins/liveshot_comments/liveshot_comments.php';
	targetOutput = jQuery(obj).parents('.discussions').next('.children');
	exp_col = jQuery(obj).parent('.exp-col');
	if( jQuery(targetOutput).hasClass('hasContent')==false){
  	jQuery.get(postUrl,values,
  		function(data) {
  			if(data.success==true){
  				if (targetOutput.length==0) {
  					targetOutput = jQuery(obj).parents('.discussions');
  					jQuery(targetOutput).after('<div class="children hasContent">'+data.output+'</div>');
  					slideDownDiv = jQuery(obj).parents('.discussions').next('.children');
  				  jQuery(slideDownDiv).slideDown('normal');
  				  jQuery(exp_col).children('a.expand').hide();
  				  jQuery(exp_col).append('<a class="collapse" href="#" style="display:block;" onclick="collapseReplies(this);return false;"><img src="/wp-content/themes/liveshot/images/collapse.jpg" /></a>');
  				}else {
  					jQuery(targetOutput).addClass('hasContent');
  					jQuery(targetOutput).replaceWith('<div class="children hasContent">'+data.output+'</div>');
  				  jQuery(targetOutput).slideDown('normal');
  				  jQuery(exp_col).children('a.expand').hide();
  				  jQuery(exp_col).append('<a class="collapse" href="#" style="display:block;" onclick="collapseReplies(this);return false;"><img src="/wp-content/themes/liveshot/images/collapse.jpg" /></a>');
  				} 
  			}
  		}	
  	,'json');
	}else{
		jQuery(targetOutput).slideDown('normal');
	  jQuery(exp_col).children('a.expand').hide();
	  jQuery(exp_col).children('a.collapse').show();
	}
	return false;
}

function collapseReplies(obj) {
	collapseTarget = jQuery(obj).parents('.discussions').next('.children');
	exp_col = jQuery(obj).parent('.exp-col');
	jQuery(collapseTarget).slideUp('normal');
	jQuery(exp_col).children('a.collapse').hide();
	jQuery(exp_col).children('a.expand').show();
}
